package com.example.mydy.withdraw;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.example.mydy.R;
import com.google.android.material.textfield.TextInputEditText;

public class WithdrawStep2Fragment extends Fragment implements WithdrawStepListener {

    private static final String PREFS_NAME = "MyAppPrefs";
    private static final String KEY_ALIPAY_ACCOUNT = "alipay_account";
    private static final String KEY_ALIPAY_NAME = "alipay_name";

    private TextView tvAlipayAccount;
    private TextView tvAlipayName;
    private Button btnEditAlipay;
    
    private String alipayAccount = "";
    private String alipayName = "";
    private WithdrawDataManager dataManager;
    private SharedPreferences prefs;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_withdraw_step2, container, false);
        
        // 初始化视图
        tvAlipayAccount = view.findViewById(R.id.tv_alipay_account);
        tvAlipayName = view.findViewById(R.id.tv_alipay_name);
        btnEditAlipay = view.findViewById(R.id.btn_edit_alipay);
        
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        // 获取数据管理器
        dataManager = WithdrawDataManager.getInstance();
        
        // 获取SharedPreferences
        prefs = requireActivity().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        
        // 加载支付宝账户信息
        loadAlipayInfo();
        
        // 设置编辑按钮点击事件
        btnEditAlipay.setOnClickListener(v -> {
            showAlipayInfoDialog();
        });
    }

    private void loadAlipayInfo() {
        // 尝试从数据管理器获取数据
        alipayAccount = dataManager.getAlipayAccount();
        alipayName = dataManager.getAlipayName();
        
        // 如果数据管理器中没有数据，从SharedPreferences中获取
        if (TextUtils.isEmpty(alipayAccount) || TextUtils.isEmpty(alipayName)) {
            alipayAccount = prefs.getString(KEY_ALIPAY_ACCOUNT, "");
            alipayName = prefs.getString(KEY_ALIPAY_NAME, "");
            
            // 更新数据管理器
            dataManager.setAlipayAccount(alipayAccount);
            dataManager.setAlipayName(alipayName);
        }
        
        // 更新UI显示
        updateAlipayInfo();
    }

    private void updateAlipayInfo() {
        if (!TextUtils.isEmpty(alipayAccount)) {
            // 显示部分隐藏的支付宝账号
            if (alipayAccount.length() > 7) {
                String maskedAccount = alipayAccount.substring(0, 3) + "****" + 
                        alipayAccount.substring(alipayAccount.length() - 4);
                tvAlipayAccount.setText(maskedAccount);
            } else {
                tvAlipayAccount.setText(alipayAccount);
            }
        } else {
            tvAlipayAccount.setText("未设置");
        }
        
        if (!TextUtils.isEmpty(alipayName)) {
            // 显示部分隐藏的姓名
            if (alipayName.length() > 1) {
                String maskedName = alipayName.substring(0, 1) + "*" + 
                        (alipayName.length() > 2 ? alipayName.substring(alipayName.length() - 1) : "");
                tvAlipayName.setText(maskedName);
            } else {
                tvAlipayName.setText(alipayName);
            }
        } else {
            tvAlipayName.setText("未设置");
        }
    }

    private void showAlipayInfoDialog() {
        // 创建对话框视图
        View dialogView = LayoutInflater.from(requireContext()).inflate(R.layout.dialog_withdrawal_info, null);
        
        // 初始化对话框视图
        TextInputEditText etAlipayAccount = dialogView.findViewById(R.id.et_alipay_account);
        TextInputEditText etAlipayName = dialogView.findViewById(R.id.et_alipay_name);
        
        // 设置当前值
        etAlipayAccount.setText(alipayAccount);
        etAlipayName.setText(alipayName);
        
        AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
        builder.setTitle("支付宝账户信息")
                .setView(dialogView)
                .setPositiveButton("保存", (dialog, which) -> {
                    // 获取输入值
                    String account = etAlipayAccount.getText().toString().trim();
                    String name = etAlipayName.getText().toString().trim();
                    
                    // 验证输入
                    if (TextUtils.isEmpty(account)) {
                        Toast.makeText(requireContext(), "请输入支付宝账号", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    
                    if (TextUtils.isEmpty(name)) {
                        Toast.makeText(requireContext(), "请输入支付宝实名", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    
                    // 保存数据
                    alipayAccount = account;
                    alipayName = name;
                    
                    // 保存到SharedPreferences
                    prefs.edit()
                            .putString(KEY_ALIPAY_ACCOUNT, alipayAccount)
                            .putString(KEY_ALIPAY_NAME, alipayName)
                            .apply();
                    
                    // 更新数据管理器
                    dataManager.setAlipayAccount(alipayAccount);
                    dataManager.setAlipayName(alipayName);
                    
                    // 更新UI
                    updateAlipayInfo();
                    
                    Toast.makeText(requireContext(), "支付宝信息已保存", Toast.LENGTH_SHORT).show();
                })
                .setNegativeButton("取消", null)
                .show();
    }

    @Override
    public boolean validate() {
        // 验证支付宝账户信息
        if (TextUtils.isEmpty(alipayAccount) || TextUtils.isEmpty(alipayName)) {
            Toast.makeText(requireContext(), "请设置支付宝账户信息", Toast.LENGTH_SHORT).show();
            return false;
        }
        
        return true;
    }
}